#include "worker.h"
#include "taskQueue.h"
#include "threadPool.h"



int exitPipe[2];
void handler(int signum){
    printf("signum = %d\n", signum);
  int retw=  write(exitPipe[1],"1",1);
}
int main(int argc, char *argv[])
{
    // ./server 192.168.72.128 1234 4
    ARGS_CHECK(argc,4);

    struct sockaddr_in client_addr;
    socklen_t addr_len = sizeof(client_addr);
    uint16_t client_port;
    char ip_str[INET_ADDRSTRLEN];

   int retp= pipe(exitPipe);
    if(fork()!= 0){
        close(exitPipe[0]);
        signal(SIGUSR1,handler);
        wait(NULL);
        printf("Parent is going to exit!\n");
        exit(0);
    }
    // 只有子进程 才能创建线程池
    close(exitPipe[1]);
    threadPool_t threadPool;
    threadPoolInit(&threadPool, atoi(argv[3]));
    makeWorker(&threadPool);

    int sockfd;
    tcpInit(argv[1],argv[2],&sockfd);
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd);
    epollAdd(epfd,exitPipe[0]);
    while(1){
        struct epoll_event readySet[1024];
        int readyNum = epoll_wait(epfd,readySet,1024,-1);
        for(int i = 0; i < readyNum; ++i){
            if(readySet[i].data.fd == sockfd){
                int netfd= accept(sockfd, (struct sockaddr*)&client_addr, &addr_len);
                inet_ntop(AF_INET, &client_addr.sin_addr, ip_str, INET_ADDRSTRLEN);
                client_port = ntohs(client_addr.sin_port);
                //Todo 将端口和ip一起放入任务队列；
                //修改任务队列节点数据类型
                //初始化入队节点节点信息，netfd，clienet_addr，client_port
                netinfo_t netinfo;
                bzero(&netinfo,sizeof(netinfo_t));
                memcpy(netinfo.ip,ip_str,sizeof(ip_str));
                netinfo.port = client_port;
                

                printf("I got 1 task!\n");
                // 分配任务
                pthread_mutex_lock(&threadPool.mutex);
                //入队由入队netfd入队jie结构体
                enQueue(&threadPool.taskQueue,netfd,netinfo);
                printf("I am master, I send a netfd  = %d\n", netfd);
                pthread_cond_signal(&threadPool.cond);
                pthread_mutex_unlock(&threadPool.mutex);
            }
            else if(readySet[i].data.fd == exitPipe[0]){
                // 线程池知道信号来了
                printf("threadPool is going to exit!\n");
                //for(int j = 0; j < threadPool.tidArr.workerNum; ++j){
                //    pthread_cancel(threadPool.tidArr.arr[j]);
                //    
                //}
                pthread_mutex_lock(&threadPool.mutex);
                threadPool.exitFlag = 1;
                pthread_cond_broadcast(&threadPool.cond);
                pthread_mutex_unlock(&threadPool.mutex);
                for(int j = 0; j < threadPool.tidArr.workerNum; ++j){
                    pthread_join(threadPool.tidArr.arr[j],NULL);
                }
                printf("main thread is going to exit!\n");
                exit(0);
            }
        }
    }
    return 0;
}

